/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string | null}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string')) {
      if ((/^[0-9]+$/.test(time))) {
        // support "1548221490638"
        time = parseInt(time)
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/')
      }
    }

    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    return value.toString().padStart(2, '0')
  })
  return time_str
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (('' + time).length === 10) {
    time = parseInt(time) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

/** *
 *
 *  将列表型的数据转化成树形数据 => 递归算法 => 自身调用自身 => 一定条件不能一样， 否则就会死循环
 *  遍历树形 有一个重点 要先找一个头儿
 * ***/

export function tranListTotreeData(list, rootValue) {
  var arr = []
  list.forEach(item => {
    if (item.parent_id === rootValue) {
      // 找到之后 就要去找 item 下面有没有子节点
      const children = tranListTotreeData(list, item.id)
      if (children.length) {
        // 如果children的长度大于0 说明找到了子节点
        item.children = children
      }
      arr.push(item)// 将内容加入到数组中
    }
  })
  return arr
}
export function tranListTotreeDatapid(list, rootValue) {
  var arr = []
  list.forEach(item => {
    if (item.pid === rootValue) {
      if (item.sub_name !== '' || item.sub_name !== undefined) {
        item.name = item.name + '(' + item.sub_name + ')'
      }
      // 找到之后 就要去找 item 下面有没有子节点
      const children = tranListTotreeDatapid(list, item.id)
      if (children.length) {
        // 如果children的长度大于0 说明找到了子节点
        item.children = children
      }
      arr.push(item)// 将内容加入到数组中
    }
  })
  return arr
}
export function tranListTotreeDatCate(list, rootValue) {
  var arr = []
  list.forEach(item => {
    if (item.pid === rootValue) {
      // 找到之后 就要去找 item 下面有没有子节点
      const children = tranListTotreeDatCate(list, item.id)
      if (children.length) {
        // 如果children的长度大于0 说明找到了子节点
        item.children = children
      }
      arr.push(item)// 将内容加入到数组中
    }
  })
  return arr
}
export function qytranListTotreeDatCate(list, rootValue) {
  var arr = []
  list.forEach(item => {
    if (item.pid === rootValue) {
      // 找到之后 就要去找 item 下面有没有子节点
      const children = qytranListTotreeDatCate(list, item.qyid)
      if (children.length) {
        // 如果children的长度大于0 说明找到了子节点
        item.children = children
      }
      arr.push(item)// 将内容加入到数组中
    }
  })
  return arr
}
// 规格组合4层
export function convertSpecsListToSpecListVal(specsList) {
  const specListVal = []// 定义了一个空数组
  const timestamp = new Date().getTime()// 获取当前时间戳
  if (specsList.length === 1) {
    // 循环一层
    for (var i = 0; i < specsList[0].items.length; i++) {
      const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
      const item = specsList[0].items[i]
      combo.namelist.push({ name: item.title, rowspan: 1 })
      combo.ks = specsList[0].items[i].k.toString()
      combo.barcode = timestamp + '-' + [i]
      specListVal.push(combo)
    }
  } else if (specsList.length === 2) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (var j = 0; j < specsList[1].items.length; j++) {
        const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
        if (j === 0) {
          combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length })
        }
        combo.namelist.push({ name: specsList[1].items[j].title, rowspan: 1 })
        combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k
        combo.barcode = timestamp + '-' + [i] + [j]
        specListVal.push(combo)
      }
    }
  } else if (specsList.length === 3) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
          if (j === 0 && n === 0) {
            combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length })
          }
          if (n === 0) {
            combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length })
          }
          combo.namelist.push({ name: specsList[2].items[n].title, rowspan: 1 })
          combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k
          combo.barcode = timestamp + '-' + [i] + [j] + [n]
          specListVal.push(combo)
        }
      }
    }
  } else if (specsList.length === 4) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          for (let m = 0; m < specsList[3].items.length; m++) {
            const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
            if (j === 0 && n === 0 && m === 0) {
              combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length * specsList[3].items.length })
            }
            if (n === 0 & m === 0) {
              combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length * specsList[3].items.length })
            }
            if (m === 0) {
              combo.namelist.push({ name: specsList[2].items[n].title, rowspan: specsList[3].items.length })
            }
            combo.namelist.push({ name: specsList[3].items[m].title, rowspan: 1 })
            combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k + ',' + specsList[3].items[m].k
            combo.barcode = timestamp + '-' + [i] + [j] + [n] + [m]
            specListVal.push(combo)
          }
        }
      }
    }
  }
  return specListVal
}

// 规格组合4层
export function specListValFor(specsList) {
  const specListVal = []// 定义了一个空数组
  if (specsList.length === 1) {
    // 循环一层
    for (var i = 0; i < specsList[0].items.length; i++) {
      const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
      const item = specsList[0].items[i]
      combo.namelist.push({ name: item.title, rowspan: 1 })
      combo.ks = specsList[0].items[i].k
      specListVal.push(combo)
    }
  } else if (specsList.length === 2) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (var j = 0; j < specsList[1].items.length; j++) {
        const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
        combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length })
        combo.namelist.push({ name: specsList[1].items[j].title, rowspan: 1 })
        combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k
        specListVal.push(combo)
      }
    }
  } else if (specsList.length === 3) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
          combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length })
          combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length })
          combo.namelist.push({ name: specsList[2].items[n].title, rowspan: 1 })
          combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k
          specListVal.push(combo)
        }
      }
    }
  } else if (specsList.length === 4) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          for (let m = 0; m < specsList[3].items.length; m++) {
            const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, givescore: 0, barcode: '', ks: '' }
            combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length * specsList[3].items.length })
            combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length * specsList[3].items.length })
            combo.namelist.push({ name: specsList[2].items[n].title, rowspan: specsList[3].items.length })
            combo.namelist.push({ name: specsList[3].items[m].title, rowspan: 1 })
            combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k + ',' + specsList[3].items[m].k
            specListVal.push(combo)
          }
        }
      }
    }
  }
  return specListVal
}

// 规格组合4层(编辑渲染)
export function editSpecsListToSpecListVal(specsList, data) {
  const specListVal = []// 定义了一个空数组
  let cur = 0
  if (specsList.length === 1) {
    // 循环一层
    for (var i = 0; i < specsList[0].items.length; i++) {
      const combo = { namelist: [], pic: data[i].pic, market_price: data[i].market_price, cost_price: data[i].cost_price, sell_price: data[i].sell_price, weight: data[i].weight, stock: data[i].stock, givescore: data[i].givescore, barcode: data[i].barcode, ks: data[i].ks }
      const item = specsList[0].items[i]
      combo.namelist.push({ name: item.title, rowspan: 1 })
      specListVal.push(combo)
    }
  } else if (specsList.length === 2) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (var j = 0; j < specsList[1].items.length; j++) {
        const combo = { namelist: [], pic: data[cur].pic, market_price: data[cur].market_price, cost_price: data[cur].cost_price, sell_price: data[cur].sell_price, weight: data[cur].weight, stock: data[cur].stock, givescore: data[cur].givescore, barcode: data[cur].barcode, ks: data[cur].ks }
        if (j === 0) {
          combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length })
        }
        combo.namelist.push({ name: specsList[1].items[j].title, rowspan: 1 })
        specListVal.push(combo)
        cur++
      }
    }
  } else if (specsList.length === 3) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          const combo = { namelist: [], pic: '', market_price: data[cur].market_price, cost_price: data[cur].cost_price, sell_price: data[cur].sell_price, weight: data[cur].weight, stock: data[cur].stock, givescore: data[cur].givescore, barcode: data[cur].barcode, ks: data[cur].ks }
          if (j === 0 && n === 0) {
            combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length })
          }
          if (n === 0) {
            combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length })
          }
          combo.pic = data[cur].pic
          combo.namelist.push({ name: specsList[2].items[n].title, rowspan: 1 })
          specListVal.push(combo)
          cur++
        }
      }
    }
  } else if (specsList.length === 4) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          for (let m = 0; m < specsList[3].items.length; m++) {
            const combo = { namelist: [], pic: data[cur].pic, market_price: data[cur].market_price, cost_price: data[cur].cost_price, sell_price: data[cur].sell_price, weight: data[cur].weight, stock: data[cur].stock, givescore: data[cur].givescore, barcode: data[cur].barcode, ks: data[cur].ks }
            if (j === 0 && n === 0 && m === 0) {
              combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length * specsList[3].items.length })
            }
            if (n === 0 & m === 0) {
              combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length * specsList[3].items.length })
            }
            if (m === 0) {
              combo.namelist.push({ name: specsList[2].items[n].title, rowspan: specsList[3].items.length })
            }
            combo.namelist.push({ name: specsList[3].items[m].title, rowspan: 1 })
            specListVal.push(combo)
            cur++
          }
        }
      }
    }
  }
  return specListVal
}
// 规格组合4层
export function convertSpecsListToSpecListValFu(specsList) {
  const specListVal = []// 定义了一个空数组
  const timestamp = new Date().getTime()// 获取当前时间戳
  // 循环一层
  for (var i = 0; i < specsList[0].items.length; i++) {
    const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, stock: [], givescore: 0, barcode: '', ks: '' }
    const item = specsList[0].items[i]
    combo.namelist.push({ name: item.title, rowspan: 1 })
    combo.ks = specsList[0].items[i].k.toString()
    combo.barcode = timestamp + '-' + [i]
    specListVal.push(combo)
  }

  return specListVal
}
// 规格组合4层
export function specListValForFu(specsList) {
  const specListVal = []// 定义了一个空数组
  // 循环一层
  for (var i = 0; i < specsList[0].items.length; i++) {
    const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, stock: [], givescore: 0, barcode: '', ks: '' }
    const item = specsList[0].items[i]
    combo.namelist.push({ name: item.title, rowspan: 1 })
    combo.ks = specsList[0].items[i].k
    specListVal.push(combo)
  }
  return specListVal
}
// 服务规格组合(编辑渲染)
export function editSpecsListToSpecListValFu(specsList, data) {
  const specListVal = []// 定义了一个空数组
  // 循环一层
  for (var i = 0; i < specsList[0].items.length; i++) {
    const combo = { namelist: [], pic: data[i].pic, market_price: data[i].market_price, cost_price: data[i].cost_price, sell_price: data[i].sell_price, stock: data[i].stock, givescore: data[i].givescore, barcode: data[i].barcode, ks: data[i].ks }
    const item = specsList[0].items[i]
    combo.namelist.push({ name: item.title, rowspan: 1 })
    specListVal.push(combo)
  }
  return specListVal
}
// 定义一个函数将字符串转为数字数组
export function ArrNumne(str) {
  const NewArr = str.split(',')
  NewArr.forEach((item, index) => {
    NewArr[index] = parseInt(NewArr[index])
  })
  return NewArr
}

// 金币规格组合4层
export function speciesSpecListValFor(specsList) {
  const specListVal = []// 定义了一个空数组
  if (specsList.length === 1) {
    // 循环一层
    for (var i = 0; i < specsList[0].items.length; i++) {
      const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
      const item = specsList[0].items[i]
      combo.namelist.push({ name: item.title, rowspan: 1 })
      combo.ks = specsList[0].items[i].k
      specListVal.push(combo)
    }
  } else if (specsList.length === 2) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (var j = 0; j < specsList[1].items.length; j++) {
        const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
        combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length })
        combo.namelist.push({ name: specsList[1].items[j].title, rowspan: 1 })
        combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k
        specListVal.push(combo)
      }
    }
  } else if (specsList.length === 3) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
          combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length })
          combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length })
          combo.namelist.push({ name: specsList[2].items[n].title, rowspan: 1 })
          combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k
          specListVal.push(combo)
        }
      }
    }
  } else if (specsList.length === 4) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          for (let m = 0; m < specsList[3].items.length; m++) {
            const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
            combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length * specsList[3].items.length })
            combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length * specsList[3].items.length })
            combo.namelist.push({ name: specsList[2].items[n].title, rowspan: specsList[3].items.length })
            combo.namelist.push({ name: specsList[3].items[m].title, rowspan: 1 })
            combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k + ',' + specsList[3].items[m].k
            specListVal.push(combo)
          }
        }
      }
    }
  }
  return specListVal
}

// 金币规格组合4层
export function speciesConvertSpecsListToSpecListVal(specsList) {
  const specListVal = []// 定义了一个空数组
  const timestamp = new Date().getTime()// 获取当前时间戳
  if (specsList.length === 1) {
    // 循环一层
    for (var i = 0; i < specsList[0].items.length; i++) {
      const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
      const item = specsList[0].items[i]
      combo.namelist.push({ name: item.title, rowspan: 1 })
      combo.ks = specsList[0].items[i].k.toString()
      combo.barcode = timestamp + '-' + [i]
      specListVal.push(combo)
    }
  } else if (specsList.length === 2) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (var j = 0; j < specsList[1].items.length; j++) {
        const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
        if (j === 0) {
          combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length })
        }
        combo.namelist.push({ name: specsList[1].items[j].title, rowspan: 1 })
        combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k
        combo.barcode = timestamp + '-' + [i] + [j]
        specListVal.push(combo)
      }
    }
  } else if (specsList.length === 3) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
          if (j === 0 && n === 0) {
            combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length })
          }
          if (n === 0) {
            combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length })
          }
          combo.namelist.push({ name: specsList[2].items[n].title, rowspan: 1 })
          combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k
          combo.barcode = timestamp + '-' + [i] + [j] + [n]
          specListVal.push(combo)
        }
      }
    }
  } else if (specsList.length === 4) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          for (let m = 0; m < specsList[3].items.length; m++) {
            const combo = { namelist: [], pic: '', market_price: undefined, cost_price: undefined, sell_price: undefined, weight: undefined, stock: 1000, barcode: '', ks: '' }
            if (j === 0 && n === 0 && m === 0) {
              combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length * specsList[3].items.length })
            }
            if (n === 0 & m === 0) {
              combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length * specsList[3].items.length })
            }
            if (m === 0) {
              combo.namelist.push({ name: specsList[2].items[n].title, rowspan: specsList[3].items.length })
            }
            combo.namelist.push({ name: specsList[3].items[m].title, rowspan: 1 })
            combo.ks = specsList[0].items[i].k + ',' + specsList[1].items[j].k + ',' + specsList[2].items[n].k + ',' + specsList[3].items[m].k
            combo.barcode = timestamp + '-' + [i] + [j] + [n] + [m]
            specListVal.push(combo)
          }
        }
      }
    }
  }
  return specListVal
}

// 金币规格组合4层(编辑渲染)
export function speciesEditSpecsListToSpecListVal(specsList, data) {
  const specListVal = []// 定义了一个空数组
  let cur = 0
  if (specsList.length === 1) {
    // 循环一层
    for (var i = 0; i < specsList[0].items.length; i++) {
      const combo = { namelist: [], pic: data[i].pic, market_price: data[i].market_price, cost_price: data[i].cost_price, sell_price: data[i].sell_price, weight: data[i].weight, stock: data[i].stock, barcode: data[i].barcode, ks: data[i].ks }
      const item = specsList[0].items[i]
      combo.namelist.push({ name: item.title, rowspan: 1 })
      specListVal.push(combo)
    }
  } else if (specsList.length === 2) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (var j = 0; j < specsList[1].items.length; j++) {
        const combo = { namelist: [], pic: data[cur].pic, market_price: data[cur].market_price, cost_price: data[cur].cost_price, sell_price: data[cur].sell_price, weight: data[cur].weight, stock: data[cur].stock, barcode: data[cur].barcode, ks: data[cur].ks }
        if (j === 0) {
          combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length })
        }
        combo.namelist.push({ name: specsList[1].items[j].title, rowspan: 1 })
        specListVal.push(combo)
        cur++
      }
    }
  } else if (specsList.length === 3) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          const combo = { namelist: [], pic: '', market_price: data[cur].market_price, cost_price: data[cur].cost_price, sell_price: data[cur].sell_price, weight: data[cur].weight, stock: data[cur].stock, barcode: data[cur].barcode, ks: data[cur].ks }
          if (j === 0 && n === 0) {
            combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length })
          }
          if (n === 0) {
            combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length })
          }
          combo.pic = data[cur].pic
          combo.namelist.push({ name: specsList[2].items[n].title, rowspan: 1 })
          specListVal.push(combo)
          cur++
        }
      }
    }
  } else if (specsList.length === 4) {
    // 循环两层
    for (let i = 0; i < specsList[0].items.length; i++) {
      const item = specsList[0].items[i]
      for (let j = 0; j < specsList[1].items.length; j++) {
        for (let n = 0; n < specsList[2].items.length; n++) {
          for (let m = 0; m < specsList[3].items.length; m++) {
            const combo = { namelist: [], pic: data[cur].pic, market_price: data[cur].market_price, cost_price: data[cur].cost_price, sell_price: data[cur].sell_price, weight: data[cur].weight, stock: data[cur].stock, barcode: data[cur].barcode, ks: data[cur].ks }
            if (j === 0 && n === 0 && m === 0) {
              combo.namelist.push({ name: item.title, rowspan: specsList[1].items.length * specsList[2].items.length * specsList[3].items.length })
            }
            if (n === 0 & m === 0) {
              combo.namelist.push({ name: specsList[1].items[j].title, rowspan: specsList[2].items.length * specsList[3].items.length })
            }
            if (m === 0) {
              combo.namelist.push({ name: specsList[2].items[n].title, rowspan: specsList[3].items.length })
            }
            combo.namelist.push({ name: specsList[3].items[m].title, rowspan: 1 })
            specListVal.push(combo)
            cur++
          }
        }
      }
    }
  }
  return specListVal
}
/**
 * Check if an element has a class
 * @param {HTMLElement} elm
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele, cls) {
  return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
}

/**
 * Add class to element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
export function addClass(ele, cls) {
  if (!hasClass(ele, cls)) ele.className += ' ' + cls
}

/**
 * Remove class from element
 * @param {HTMLElement} elm
 * @param {string} cls
 */
export function removeClass(ele, cls) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
    ele.className = ele.className.replace(reg, ' ')
  }
}

/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}
